﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MotorcycleStore.Repository;
using MotorcycleStore.DomainModel;

namespace MotorcycleStore.MVC.Areas.Administration.Controllers
{
	public class ManufacturerController : Controller
	{
		IManufacturerRepository repository;

		public ManufacturerController(IManufacturerRepository repository)
		{
			this.repository = repository;
		}

		//
		// GET: /Administration/Manufacturer/

		public ActionResult Index()
		{
			return View(repository.Manufacturers);
		}

		//
		// GET: /Administration/Manufacturer/Create

		public ViewResult Create()
		{
			return View();
		}

		//
		// POST: /Administration/Manufactuurer/Create

		[HttpPost]
		public ActionResult Create(Manufacturer manufacturer)
		{
			if (ModelState.IsValid)
			{
				repository.Add(manufacturer);
				repository.Save();

				if (Request.IsAjaxRequest())
				{
					var manufacturers = repository.Manufacturers;
					return PartialView("ManufacturerList", manufacturers);
				}

				TempData["MessageTitle"] = String.Format("Manufacturer Created");
				TempData["MessageContent"] = String.Format("{0} was created and saved successfully.", manufacturer.Name);

				return RedirectToAction("Index");
			}

			return View(manufacturer);
		}

		//
		// GET: /Administration/Manufacturer/Delete/5
		public ViewResult Delete(int id)
		{
			var manufacturer = repository.Manufacturers.SingleOrDefault(m => m.Id == id);
			if (manufacturer == null)
				return View("NotFound");

			return View(manufacturer);
		}

		//
		// POST: /Administration/Manufacturer/Delete/5
		[HttpPost]
		public ActionResult Delete(int id, string confirmButton)
		{
			var manufacturer = repository.Manufacturers.SingleOrDefault(m => m.Id == id);
			if (manufacturer == null)
				return View("NotFound");

			repository.Delete(id);
			repository.Save();

			if (Request.IsAjaxRequest())
			{
				return null;
			}

			TempData["MessageTitle"] = String.Format("Manufacturer Deleted");
			TempData["MessageContent"] = String.Format("{0} was deleted.", manufacturer.Name);

			return RedirectToAction("Index");
		}

		//
		// GET: /Administration/Manufacturer/Edit/5
		public ViewResult Edit(int id)
		{
			var manufacturer = repository.Manufacturers.SingleOrDefault(m => m.Id == id);
			if (manufacturer == null)
				return View("NotFound");
			return View(manufacturer);
		}

		//
		// POST: /Administration/Manufacturer/Edit/5
		[HttpPost]
		public ActionResult Edit(int id, FormCollection form)
		{
			var manufacturer = repository.Manufacturers.SingleOrDefault(m => m.Id == id);
			if (manufacturer == null)
				return View("NotFound");
			try
			{
				UpdateModel(manufacturer, new string[] { "Name", "Website" });
				if (ModelState.IsValid)
				{
					repository.Save();
					TempData["MessageTitle"] = "Manufacturer Saved";
					TempData["MessageContent"] = String.Format("{0} was saved", manufacturer.Name);
				}
			}
			catch (InvalidOperationException)
			{
			}

			return View(manufacturer);
		}

		//
		// GET: /Administration/Manufacturer/Details/5
		public ViewResult Details(int id)
		{
			var manufacturer = repository.Manufacturers.SingleOrDefault(m => m.Id == id);
			return View(manufacturer);
		}

		//
		// GET: /Administration/Manufacturer/ManufacturerForm
		public PartialViewResult ManufacturerForm()
		{
			return PartialView();
		}

		//
		// GET: /Administration/Manufacturer/IsUnique?name=Suzuki
		public string IsUnique(string name)
		{
			bool exists = repository.Manufacturers.Any(m => m.Name == name);
			return (exists ? "false" : "true");
		}
	}
}
